﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 入院 : Form
    {
        SqlConnection conn = new SqlConnection(DBHelper.connString);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public 入院()
        {
            InitializeComponent();
        }
        public void Fill()
        {
            string sql = "select Patient.HosNo,PatName,PatSex,BronYear,PatId,PatAdr from Patient";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void 入院_Load(object sender, EventArgs e)
        {
            Fill();
        }

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_bqzt.Text = "";
            tb_ch.Text = "";
            tb_fb.Text = "";
            tb_ks.Text = "";
            tb_nl.Text = "";
            dtp_ryrq.Text = "";
            dtp_ssrq.Text = "";
            tb_szzb.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_yjje.Text = "";
            tb_ys.Text = "";
            tb_zd.Text = "";
            tb_zrhs.Text = "";
            tb_ZYNo.Text = "";
            cb_hldj.Text = "";
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
             DateTime dt=System .DateTime .Now ;
           int  year=dt.Year;
           string HosNo = tb_ZYNo.Text.Trim();	
            string PatName=	tb_xm.Text.Trim ();
            string PatSex= tb_xb.Text.Trim ();	
            string PatAge=tb_nl.Text.Trim ();
            int Age=Convert .ToInt32 (PatAge );
            int ds = year - Age;
            string BronYear = ds.ToString();
            string BedNo=tb_ch.Text.Trim ();		
            DateTime  HosIndate=dtp_ryrq.Value ;	
            string HosArea=tb_szzb.Text.Trim ();		
            string HosPrepay= tb_yjje.Text.Trim ();	
            string PatType=tb_fb.Text.Trim ();	
            DateTime  HosSurgerydate= dtp_ssrq.Value ;		
            string HosRank=cb_hldj.Text .Trim ();	
            string HosCondition=tb_bqzt.Text.Trim ();		
            string HosIllness=tb_zd.Text.Trim ();
            string DName = tb_ys.Text.Trim();
            string NName=  tb_zrhs.Text.Trim ();
            string a=tb_ch.Text.Trim ();
            int   b = a .Length ;
            string c = a.Substring(0, (b - 2));
            string BedRoom = c;
                string BedSubject=tb_ks .Text .Trim ();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string  DNo="select DocNo from Doctor where DocName =DName ";
                string NNo = "select NurNo from Nurse where NurName =NName ";
                string sql = string.Format("insert into Patient values ('{0}','{1}','{2}','{3}','{4}','{5}')", HosNo, PatName, PatSex, BronYear,null  ,null  );
                string str = string.Format("insert into InHospital values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')", HosNo, BedNo, HosIndate, HosArea, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness, null ,null ,DNo, NNo); 
                string abc= string .Format ("insert into Bed values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", BedNo, BedRoom,BedSubject,null ,null ,null ,null ,null );
                 SqlCommand cmd = new SqlCommand(sql, conn);
                 SqlCommand com = new SqlCommand(str, conn);
                 SqlCommand bed = new SqlCommand(abc, conn);
                conn.Open();
                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m= Convert.ToInt32(com.ExecuteNonQuery());
                int z = Convert.ToInt32(bed.ExecuteNonQuery());
                if (n == 1&& m==1&&z==1)
                {
                    MessageBox.Show("保存成功！");
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }

        }
     
       
       

    }
}
